<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeDialog()">
    <!-- 表单 -->
    <el-form label-width="120px" :model="formData" :rules="rules" ref="form">
      <el-form-item label="姓名" prop="username">
        <el-input
          style="width: 50%"
          placeholder="请输入姓名"
          v-model="formData.username"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          style="width: 50%"
          placeholder="请输入手机号"
          v-model="formData.mobile"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          style="width: 50%"
          placeholder="请选择入职时间"
          v-model="formData.timeOfEntry"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          style="width: 50%"
          placeholder="请选择"
          v-model="formData.formOfEmployment"
        >
          <el-option
            v-for="(item, index) in value"
            :key="index"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          style="width: 50%"
          placeholder="请输入工号"
          v-model="formData.workNumber"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          style="width: 50%"
          placeholder="请选择部门"
          v-model="formData.departmentName"
          @focus="getBumenList"
        />
        <div class="tree-box">
          <el-tree
            v-if="showTree"
            :data="bumenList"
            :props="{ label: 'name' }"
            @node-click="handleNodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          style="width: 50%"
          placeholder="请选择转正时间"
          v-model="formData.correctionTime"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click.native="closeDialog">取消</el-button>
      <el-button type="primary" @click.native.prevent="adduser">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { Addemployee } from '@/api/employess'
import { tranListToTreeData } from '@/utils/translist'
import { getDepartments } from '@/api/departments'
export default {
  // 组件名称
  name: 'addemployess',
  // 组件参数 接收来自父组件的数据
  props: {
    showDialog: {
      type: Boolean,
      default () {
        return false
      }
    }
  },
  // 局部注册的组件
  components: {},
  // 组件状态值
  created () { },
  data () {
    return {
      value: ['正式', '非正式'],
      showTree: false,
      bumenList: [],
      option: '',
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur', },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur', }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur', },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur', }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur', }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 通知父组件关闭弹出层
    closeDialog () {
      this.$emit('close', false)
    },
    // 添加员工功能
    adduser () {
      this.$refs.form.validate(valid => {
        if (valid) {
          Addemployee(this.formData).then(res => {
            this.$message.success('添加成功')
            this.$emit('update')
            this.closeDialog()
            // 重置原来的数据
            this.formData = {
              username: '',
              mobile: '',
              formOfEmployment: '',
              workNumber: '',
              departmentName: '',
              timeOfEntry: '',
              correctionTime: ''
            }
            // 重置校验结果
            this.$refs.form.resetFields()
          })
        } else {
          return false
        }
      })
    },
    // 获取部门列表方法
    getBumenList () {
      getDepartments().then(res => {
        this.bumenList = tranListToTreeData(res.data.depts, '')
        this.showTree = true // 聚焦输入框显示部门树形结构
      })
    },
    // 如果部门有下属部门就不赋值部门名称,必须选择其中一个没有下属部门的部门
    handleNodeClick (data, node) {
      if (node.childNodes.length == 0) {
        this.formData.departmentName = data.name
      }
      this.showTree = false
    }
  }
}
</script>
<style scoped>
</style>